Code
library(tidyverse)
library(lubridate)
library(zoo)
library(forecast)
library(tseries)
library(patchwork)
library(here)
library(plotly)zoo un park

uk = read.csv("../data/fx rate/UK_US.csv")
uk <- uk %>% mutate(observation_date = as.Date(observation_date))%>%
rename(UK_USD = DEXUSUK)%>%
mutate(UK_USD = na.locf(UK_USD, na.rm = FALSE))
uk_plot <- ggplot(uk, aes(x = observation_date, y = UK_USD)) +
geom_line(color = "blue") +
labs(title = "GBP/USD Exchange Rate",
x = "Date", y = "GBP(GBP per 1 USD)") +
theme_minimal()
uk_plot
jp <- read_csv("../data/fx rate/JP_US.csv") %>%
mutate(observation_date = as.Date(observation_date)) %>%
rename(JP_USD = DEXJPUS,
Date = observation_date) %>%
mutate( JP_USD = na.locf(JP_USD, na.rm = FALSE),
USD_JP = 1 / JP_USD)
jp_plot <- ggplot(jp, aes(x = Date, y = USD_JP)) +
geom_line(color = "blue") +
labs(title = "JPY/USD Exchange Rate ",
x = "Date", y = "JPY(JPY per 1 USD)") +
theme_minimal()
jp_plot
crude_oil <- read_csv(here("data/oil","oil_cleaned.csv")) %>%
mutate(Date = as.Date(Date)) %>%
arrange(Date) %>%
mutate(across(-Date, ~ na.locf(.x, na.rm = FALSE)))
ggplot(crude_oil, aes(x = Date, y = WTI)) +
geom_line(color = "darkgreen") +
labs(title = "Crude Oil Prices", x = "Date", y = "Price") +
theme_minimal()
sp500 <- read_csv(here("data/sp500","sp500_cleaned.csv")) %>%
mutate(Date = as.Date(Date)) %>%
arrange(Date) %>%
mutate(across(-Date, ~ na.locf(.x, na.rm = FALSE)))
ggplot(sp500, aes(x = Date, y = Close)) +
geom_line(color = "blue") +
labs(title = "S&P 500 Index", x = "Date", y = "Level") +
theme_minimal()

spread<- read_csv("../data/yield/merge_yield.csv")%>%
mutate(Date = as.Date(Date),
Year = lubridate::year(Date)) %>%
arrange(Date)%>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, na.rm = FALSE))) %>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, fromLast = TRUE)))
jp_plot <- plot_ly(spread, x = ~Date, y = ~JP_Spread, type = 'scatter', mode = 'lines',
name = "Japan (10Y-2Y)") %>%
layout(title = "Japan Yield Curve Spread (10Y-2Y)",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (%)"))
jp_plotspread<- read_csv("../data/yield/merge_yield.csv")%>%
mutate(Date = as.Date(Date),
Year = lubridate::year(Date)) %>%
arrange(Date)%>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, na.rm = FALSE))) %>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, fromLast = TRUE)))
eu_plot <- plot_ly(spread, x = ~Date, y = ~EU_Spread, type = 'scatter', mode = 'lines',
name = "EU (10Y-2Y)") %>%
layout(title = "EU Yield Curve Spread (10Y-2Y)",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (%)"))
eu_plotspread<- read_csv("../data/yield/merge_yield.csv")%>%
mutate(Date = as.Date(Date),
Year = lubridate::year(Date)) %>%
arrange(Date)%>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, na.rm = FALSE))) %>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, fromLast = TRUE)))
uk_plot <- plot_ly(spread, x = ~Date, y = ~UK_Spread, type = 'scatter', mode = 'lines',
name = "UK (10Y-2Y)") %>%
layout(title = "UK Yield Curve Spread (10Y-2Y)",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (%)"))
uk_plotspread<- read_csv("../data/yield/merge_yield.csv")%>%
mutate(Date = as.Date(Date),
Year = lubridate::year(Date)) %>%
arrange(Date)%>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, na.rm = FALSE))) %>%
mutate(across(ends_with("_spread"), ~ na.locf(.x, fromLast = TRUE)))
eu_plot <- plot_ly(spread, x = ~Date, y = ~US_Spread, type = 'scatter', mode = 'lines',
name = "US (10Y-2Y)") %>%
layout(title = "US Yield Curve Spread (10Y-2Y)",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (%)"))
eu_plot
Augmented Dickey-Fuller Test
data: jp_ts
Dickey-Fuller = -1.3225, Lag order = 18, p-value = 0.8653
alternative hypothesis: stationary










Augmented Dickey-Fuller Test
data: diff(log(jp_ts))
Dickey-Fuller = -18.43, Lag order = 18, p-value = 0.01
alternative hypothesis: stationary
Augmented Dickey-Fuller Test
data: diff(us_spread_ts)
Dickey-Fuller = -13.978, Lag order = 14, p-value = 0.01
alternative hypothesis: stationary
jp_df <- data.frame(Date = time(jp_ts), Value = as.numeric(jp_ts))
jp_df$MA20 <- rollmean(jp_df$Value, 20, fill = NA, align = "right")
jp_df$MA50 <- rollmean(jp_df$Value, 50, fill = NA, align = "right")
jp_df$MA100 <- rollmean(jp_df$Value, 100, fill = NA, align = "right")
plot_ly(jp_df, x = ~Date) %>%
add_lines(y = ~Value, name = "JPY Rate", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "JPY Currency Rate with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "JPY rate"))uk_df <- data.frame(Date = time(uk_ts), Value = as.numeric(uk_ts))
uk_df$MA20 <- rollmean(uk_df$Value, 20, fill = NA, align = "right")
uk_df$MA50 <- rollmean(uk_df$Value, 50, fill = NA, align = "right")
uk_df$MA100 <- rollmean(uk_df$Value, 100, fill = NA, align = "right")
plot_ly(uk_df, x = ~Date) %>%
add_lines(y = ~Value, name = "GBP Rate", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "GBP Currency Rate with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "GBP rate"))eu_df <- data.frame(Date = time(eu_ts), Value = as.numeric(eu_ts))
eu_df$MA20 <- rollmean(eu_df$Value, 20, fill = NA, align = "right")
eu_df$MA50 <- rollmean(eu_df$Value, 50, fill = NA, align = "right")
eu_df$MA100 <- rollmean(eu_df$Value, 100, fill = NA, align = "right")
plot_ly(eu_df, x = ~Date) %>%
add_lines(y = ~Value, name = "EUR Rate", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "EUR Currency Rate with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "EUR rate"))oil_df <- data.frame(Date = time(crude_oil_ts), Value = as.numeric(crude_oil_ts))
oil_df$MA20 <- rollmean(oil_df$Value, 20, fill = NA, align = "right")
oil_df$MA50 <- rollmean(oil_df$Value, 50, fill = NA, align = "right")
oil_df$MA100 <- rollmean(oil_df$Value, 100, fill = NA, align = "right")
plot_ly(oil_df, x = ~Date) %>%
add_lines(y = ~Value, name = "Oil Price", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "Crude Oil Price with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "Oil Price"))sp500_df <- data.frame(Date = time(sp500_ts), Value = as.numeric(sp500_ts))
sp500_df$MA20 <- rollmean(sp500_df$Value, 20, fill = NA, align = "right")
sp500_df$MA50 <- rollmean(sp500_df$Value, 50, fill = NA, align = "right")
sp500_df$MA100 <- rollmean(sp500_df$Value, 100, fill = NA, align = "right")
plot_ly(sp500_df, x = ~Date) %>%
add_lines(y = ~Value, name = "S&P500", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "S&P500 Index with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "S&P500"))btc_df <- data.frame(Date = time(btc_ts), Value = as.numeric(btc_ts))
btc_df$MA20 <- rollmean(btc_df$Value, 20, fill = NA, align = "right")
btc_df$MA50 <- rollmean(btc_df$Value, 50, fill = NA, align = "right")
btc_df$MA100 <- rollmean(btc_df$Value, 100, fill = NA, align = "right")
plot_ly(btc_df, x = ~Date) %>%
add_lines(y = ~Value, name = "BTC Price", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "Bitcoin Price with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "BTC Price"))jp_spread_df <- data.frame(Date = time(jp_spread_ts), Value = as.numeric(jp_spread_ts))
jp_spread_df$MA20 <- rollmean(jp_spread_df$Value, 20, fill = NA, align = "right")
jp_spread_df$MA50 <- rollmean(jp_spread_df$Value, 50, fill = NA, align = "right")
jp_spread_df$MA100 <- rollmean(jp_spread_df$Value, 100, fill = NA, align = "right")
plot_ly(jp_spread_df, x = ~Date) %>%
add_lines(y = ~Value, name = "JP Spread", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "Japan Spread with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (bps)"))eu_spread_df <- data.frame(Date = time(eu_spread_ts), Value = as.numeric(eu_spread_ts))
eu_spread_df$MA20 <- rollmean(eu_spread_df$Value, 20, fill = NA, align = "right")
eu_spread_df$MA50 <- rollmean(eu_spread_df$Value, 50, fill = NA, align = "right")
eu_spread_df$MA100 <- rollmean(eu_spread_df$Value, 100, fill = NA, align = "right")
plot_ly(eu_spread_df, x = ~Date) %>%
add_lines(y = ~Value, name = "EU Spread", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "Euro Spread with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (bps)"))uk_spread_df <- data.frame(Date = time(uk_spread_ts), Value = as.numeric(uk_spread_ts))
uk_spread_df$MA20 <- rollmean(uk_spread_df$Value, 20, fill = NA, align = "right")
uk_spread_df$MA50 <- rollmean(uk_spread_df$Value, 50, fill = NA, align = "right")
uk_spread_df$MA100 <- rollmean(uk_spread_df$Value, 100, fill = NA, align = "right")
plot_ly(uk_spread_df, x = ~Date) %>%
add_lines(y = ~Value, name = "UK Spread", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "UK Spread with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (bps)"))us_spread_df <- data.frame(Date = time(us_spread_ts), Value = as.numeric(us_spread_ts))
us_spread_df$MA20 <- rollmean(us_spread_df$Value, 20, fill = NA, align = "right")
us_spread_df$MA50 <- rollmean(us_spread_df$Value, 50, fill = NA, align = "right")
us_spread_df$MA100 <- rollmean(us_spread_df$Value, 100, fill = NA, align = "right")
plot_ly(us_spread_df, x = ~Date) %>%
add_lines(y = ~Value, name = "US Spread", line = list(color = "black")) %>%
add_lines(y = ~MA20, name = "MA20", line = list(color = "blue")) %>%
add_lines(y = ~MA50, name = "MA50", line = list(color = "red")) %>%
add_lines(y = ~MA100, name = "MA100", line = list(color = "green")) %>%
layout(title = "US Spread with Moving Averages",
xaxis = list(title = "Date"),
yaxis = list(title = "Spread (bps)"))